package com.amir.ctrl;

import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amir.model.User;
import com.amir.model.UserList;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;

@Controller
public class CTRL {

	@Autowired
	MongoOperations mongoOperations;

	@RequestMapping(value="/test.amir", method={RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody String Test(HttpServletRequest request, HttpServletResponse response){
		return "hello "+ request.getParameter("name");
	}

	@RequestMapping(value="/getAllUser.amir", method={RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody UserList getAllUser(){
		UserList userList = new UserList();

		userList.setUserList(mongoOperations.findAll(User.class));

		return userList;
	}

	@RequestMapping(value="/getByName.amir", method={RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody User getByName(@RequestParam String name){
		Query searchUserQuery = new Query(Criteria.where("name").is(name));

		return mongoOperations.findOne(searchUserQuery, User.class);
	}

	@RequestMapping(value="/saveUser.amir", method={RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody User saveUser(@RequestParam String name, @RequestParam String address, @RequestParam int id){
		User user = new User();
		user.setName(name);
		user.setAddress(address);
		user.setUid(id);
		try {
			mongoOperations.insert(user);
		} catch (DuplicateKeyException dke) {
			dke.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return user;
	}

	@RequestMapping(value="/uploadImage.amir", method={RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody String uploadImage(HttpServletRequest request){
		String name = null;

		try {
			List<FileItem> files = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
			InputStream imageStream = null;
			for(FileItem file:files){
				if(!file.isFormField()){
					imageStream = file.getInputStream();
				} else if("name".equals(file.getFieldName())){
					name = file.getString();
				}
			}

			GridFS gridFS = new GridFS(mongoOperations.getCollection("images").getDB());
			
			if(gridFS.find(name).size() > 0){
				gridFS.remove(name);
			}
			
			GridFSInputFile gfsFile = gridFS.createFile(imageStream, name);
			gfsFile.setId(name);
			gfsFile.save();
		} catch (Exception e) {
		}

		return "Image Uploaded Successfully For "+name;
	}


	@RequestMapping(value="/getImage.amir", method={RequestMethod.GET, RequestMethod.POST})
	public void getImage(@RequestParam String name, HttpServletResponse response){
		try {
			response.setContentType("image/jpeg");

			new GridFS(mongoOperations.getCollection("images").getDB()).findOne(name).writeTo(response.getOutputStream());
		} catch (Exception e) {
		}
	}
}